﻿// My methods here.
//-----------------------------------------------------------------------
// <Copyright file=Users company=DHPD>
// Copyright (c) DHPD Corporation (http://dhpd.edu.vn). All rights reserved.
// </copyright>
// <author>HP</author>
//-----------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KM.DAT;
using KM.ENT;
using KM.Models;

namespace KM.DAT
{
    /// <summary>
    /// This is datUsers class
    /// </summary>

    public class datUsers
    {
        public datUsers()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        SqlCommand cmd;
        SqlDataReader dr;
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        #region  === tạo mới và sửa Users===
        public void createEdit(entUsers entU)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_UsersCreateEdit", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("Id", entU.Id);
            cmd.Parameters.AddWithValue("UserName", entU.UserName);
            cmd.Parameters.AddWithValue("Pass", entU.Pass);
            cmd.Parameters.AddWithValue("TrangThai", entU.TrangThai);

            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion

        #region  === getall Users===
        public List<entUsers> getAll(ref PagingInfoModel page)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_UsersGetAll", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@PageIndex", page.CurrentPage);
            cmd.Parameters.AddWithValue("@PageSize", page.recordPerPage);
            cmd.Parameters.AddWithValue("@RecordCount", page.totalRecords).Direction = ParameterDirection.Output;
            dr = cmd.ExecuteReader();
            entUsers entU = new entUsers();
            List<entUsers> lst = new List<entUsers>();
            while (dr.Read())
            {
                entU = new entUsers();
                entU.Id = Convert.ToInt32(dr["Id"].ToString());
                entU.UserName = dr["UserName"].ToString();
                entU.Pass = dr["Pass"].ToString();
                entU.TrangThai = Convert.ToByte(dr["TrangThai"].ToString());

                lst.Add(entU);
            }

            dr.Close();
            if (lst.Count > 0)
            {
                // int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
                page.totalRecords = int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
            }

            sqlConnection.Close();
            return lst;
        }
        #endregion
        #region  === getall Users===
        public List<entUsers> getAllByForeignKey(int Id, ref PagingInfoModel page)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_UsersGetAllByForeignKey", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("Id", Id);
            cmd.Parameters.AddWithValue("@PageIndex", page.CurrentPage);
            cmd.Parameters.AddWithValue("@PageSize", page.recordPerPage);
            cmd.Parameters.AddWithValue("@RecordCount", page.totalRecords).Direction = ParameterDirection.Output;
            dr = cmd.ExecuteReader();
            entUsers entU = new entUsers();
            List<entUsers> lst = new List<entUsers>();
            while (dr.Read())
            {
                entU = new entUsers();
                entU.Id = Convert.ToInt32(dr["Id"].ToString());
                entU.UserName = dr["UserName"].ToString();
                entU.Pass = dr["Pass"].ToString();
                entU.TrangThai = Convert.ToByte(dr["TrangThai"].ToString());

                lst.Add(entU);
            }

            dr.Close();
            if (lst.Count > 0)
            {
                // int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
                page.totalRecords = int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
            }

            sqlConnection.Close();
            return lst;
        }
        #endregion

        #region  === getinfo Users===
        public entUsers getInfo(int Id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_UsersGetInfo", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[0].Value = Id;

            cmd.Parameters.AddRange(sqla);
            dr = cmd.ExecuteReader();
            entUsers entU = new entUsers();
            if (dr.Read())
            {
                entU.Id = Convert.ToInt32(dr["Id"].ToString());
                entU.UserName = dr["UserName"].ToString();
                entU.Pass = dr["Pass"].ToString();
                entU.TrangThai = Convert.ToByte(dr["TrangThai"].ToString());

            }
            dr.Close();
            sqlConnection.Close();
            return entU;
        }
        #endregion
        #region  === dang nhap Users===
        public entUsers getLogin(entUsers entU)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_UsersGetLogin", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("Username", entU.UserName);
            cmd.Parameters.AddWithValue("Pass", entU.Pass);
            dr = cmd.ExecuteReader();
            entU = new entUsers();
            if (dr.Read())
            {
                entU.Id = Convert.ToInt32(dr["Id"].ToString());
                entU.UserName = dr["UserName"].ToString();
                entU.Pass = dr["Pass"].ToString();

            }
            dr.Close();
            sqlConnection.Close();
            return entU;
        }
        #endregion

        #region === xoa Users===
        public void delete(int id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_UsersDeleteById", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[0].Value = id;

            cmd.Parameters.AddRange(sqla);
            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion
    }
}
